package com.arpa.ntocc.basic.api.controller;


import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.controller.BaseController;
import com.arpa.ntocc.common.domain.dto.RoleConfigDTO;
import com.arpa.ntocc.common.domain.entity.RoleConfig;
import com.arpa.ntocc.common.domain.vo.RoleConfigVO;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.ntocc.common.security.annotation.NoRepeat;
import com.arpa.ntocc.common.service.IRoleConfigService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 角色配置表 Controller
 * </p>
 *
 * @author LEO
 * @since 2019-12-05
 */
@Api(tags = "角色配置表")
@RestController
@RequestMapping(value="/roleConfig", produces = "application/json;charset=UTF-8")
@Log4j2
public class RoleConfigController extends BaseController {

   @Autowired
   private IRoleConfigService  iRoleConfigService;

    /**
     * 获取 角色配置表 列表
     *
     * @param roleConfigDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 角色配置表 列表", notes = "获取 角色配置表 列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = RoleConfigVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(RoleConfigDTO roleConfigDTO, HttpServletRequest request) {
        try {
            RoleConfigVO roleConfigVOSum = iRoleConfigService.queryListSum(roleConfigDTO);
            List<RoleConfigVO> roleConfigVOList = iRoleConfigService.queryList(roleConfigDTO);

            Page returnPage = new Page(roleConfigDTO.getPageNum(),roleConfigDTO.getPageSize());
            returnPage.setRecords(roleConfigVOList);
            returnPage.setTotal(roleConfigVOSum.getTotal());
            returnPage.setSumObject(roleConfigVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 角色配置表
     *
     * @param entity RoleConfig
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 角色配置表 ", notes = "添加 角色配置表 ")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
    @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @NoRepeat(time = 2000)
    @PostMapping()
    public Result create(RoleConfig entity, HttpServletRequest request) {
        try {
            iRoleConfigService.save(entity);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
    * 获取角色配置表详情
    *
    * @param code
    * @param request
    * @return
    */
    @ApiOperation(value = "获取角色配置表详情", notes = "获取角色配置表详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "code", value = "角色配置表code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = RoleConfig.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result roleConfig(@PathVariable String code, HttpServletRequest request) {
        try {
            RoleConfig entity = iRoleConfigService.getOne(new QueryWrapper<RoleConfig>().lambda().eq(RoleConfig::getCode,code));
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改角色配置表
     *
     * @param entityList 角色配置表
     * @param request
     * @return
     */
    @ApiOperation(value = "修改角色配置表", notes = "修改角色配置表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping()
    public Result update(@RequestBody List<RoleConfig> entityList, HttpServletRequest request) {
        if(CollectionUtils.isEmpty(entityList)){
            return Result.error("修改内容不能为空");
        }
        iRoleConfigService.updateBatchById(entityList);
        return Result.ok("更新成功！");
    }

    /**
     * 删除 角色配置表
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 角色配置表 ", notes = "删除 角色配置表 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "codes", value = "codes", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            iRoleConfigService.remove(new QueryWrapper<RoleConfig>().lambda().in(RoleConfig::getCode,codes));
            return Result.ok("操作成功！");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

 }
